package org.onejava.db;

import java.util.List;
import java.util.Map;

import org.onejava.db.mapping.TransactionMapping;

public class DB {
	
	/**
	 * 无参数普通查询
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static List<Map<String, Object>> query(String sql) throws Exception {
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.query(sql);
	}
	
	/**
	 * 有参数普通查询
	 * @param sql
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static List<Map<String, Object>> query(String sql,Map<?, ?> map) throws Exception {
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.query(sql,map);
	}
	
	/**
	 * 有参数单查询
	 * 只查询一个值，如果结果不符合，抛出错误
	 * @param sql
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static Object queryOne(String sql,Map<?, ?> map) throws Exception {
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.queryForOne(sql, map);
	}
	
	/**无参数单查询
	 * 只查询一个值，如果结果不符合，抛出错误
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static Object queryOne(String sql) throws Exception {
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.queryForOne(sql);
	}
	
	/**
	 * 无参数列查询
	 * 只查询一列值，如果结果不符合，抛出错误
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static  Map<String, Object> queryMap(String sql) throws Exception {
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.queryForMap(sql);
	}
	
	/**
	 * 有参数列查询
	 * @param sql
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static  Map<String, Object> queryMap(String sql,Map<?, ?> map) throws Exception {
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.queryForMap(sql,map);
	}
	
	/**
	 * 更新操作
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static int update(String sql) throws Exception{
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.update(sql);
	}
	/**
	 * 更新操作
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static int update(String sql,Map<?, ?> map) throws Exception{
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.update(sql,map);
	}
	
	/**
	 * 统计操作
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static int count(String sql) throws Exception{
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.getPrimaryKey(sql);
	}
	
	/**
	 * 统计操作
	 * @param sql
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static int count(String sql,Map<?, ?> map) throws Exception{
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		return sqlmapping.getPrimaryKey(sql,map);
	}
	
	/**
	 * 事务回滚
	 * @throws Exception
	 */
	public static void rollback() throws Exception{
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		sqlmapping.runRollBack();
	}
	
	/**
	 * 事务开始
	 * @throws Exception
	 */
	public static void startTran() throws Exception{
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		sqlmapping.startTran();
	}
	
	/**
	 * 事务提交
	 * @throws Exception
	 */
	public static void commit() throws Exception{
		TransactionMapping sqlmapping = TransactionMapping.getInstance();
		sqlmapping.runCommit();
	}
}
